<?php

declare(strict_types=1);

namespace App\Http\Controllers\Manage;


use App\Repositories\Manage\AdviceRepository;

class AdviceController extends ManageBaseController
{

    private $adviceRepo = NULL;

    public function __construct(AdviceRepository $adviceRepo)
    {
        parent::__construct();
        $this->adviceRepo = $adviceRepo;
    }

    public function index()
    {
        $filter = request()->all();
        $res = $this->adviceRepo->getList($filter, $this->pageNum, $this->pageSize);
        return retSuc($res);
    }

    public function detail($id)
    {
        $id = intval($id);
        if (!$id)
            return retFail("参数错误");
        $res = $this->adviceRepo->getOne($id);
        return retSuc($res);
    }

    /**
     * 清空待打印
     * @param $id
     */
    public function clear($id)
    {
        $id = intval($id);
        if (!$id)
            return retFail("参数错误");

        return $this->adviceRepo->clearPrinter($id);
    }

    /**
     * 初始化打印机
     * @param $id
     */
    public function init($id)
    {
        $id = intval($id);
        if (!$id)
            return retFail("参数错误");

        return $this->adviceRepo->initPrinter($id);
    }

    public function update($id = null)
    {
        $data = request()->only(['id', 'reply', 'status']);
        if (empty($data['id']))
            return retFail("参数错误");

        $data['handle_time'] = date("Y-m-d H:i:s");
        $last_id = $this->adviceRepo->saveOrUpdate($data, $id ?? 0);
        if (!$last_id) {
            return retFail(empty($data['id']) ? "处理成功" : "处理成功");
        }

        return retSuc([], '处理成功');
    }

    public function delete($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return retFail("参数错误");
        }

        return $this->adviceRepo->deletePrinter($id);
    }

}
